package vip.xiaonuo.smzq.core.utils;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.oss.ServiceException;

import java.io.File;

/**
 * @program: ruoyi
 * @description: 基本工具类
 * @author: 高天乐
 * @create: 2023-08-17 10:44
 **/
public class BasicUtil {

    /**
     * 字段类型转为Double类型
     * @param field
     * @return
     */
    public static Double isDouble(String field){
        if ("null".equals(field) || ObjectUtil.isEmpty(field)){
            return null;
        }
        try {
            return Double.parseDouble(field);
        }catch (Exception e){
            throw new ServiceException("字段传值错误");
        }
    }

    /**
     * 判断接收的值是否是Double类型,并判断经纬度是否正确
     * @param s 值
     * @param pattern 经度/维度正则
     * @return
     */
    public static boolean isDouble(String s,String pattern){
        if (ObjectUtil.isEmpty(s)) {
            return false;
        }
        try {
            if (ObjectUtil.isNotEmpty(pattern)){
                return s.matches(pattern);
            }
            return true;
        }catch (NumberFormatException e){
            return false;
        }
    }

    /**
     * 正则判断
     * @param s 字符串
     * @param pattern 正则表达式
     */
    public static boolean isPattern(String s,String pattern){
        return s.matches(pattern);
    }

    /**
     * 删除文件夹
     * @param folder
     */
    public static void deleteFolder(File folder){
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    deleteFolder(file);
                } else {
                    file.delete();
                }
            }
        }
        folder.delete();
    }
}
